#region Summary

// 
// Owner : JackieHan 
// 
// Solution : Skomart.Spider 
// 
// Project : Jackie.Utility
// 
// File : SpingHelper.cs
// 
// CopyRight : Copyright © 2010-2011 Skomart Corporation, All Rights Reserved 
// 
// Summary : Sping Ioc 帮助类
// 
// History : 
//			2010-09-27 17:41:36	    Create 
// 

#endregion

#region Using

using Spring.Context;
using Spring.Context.Support;

#endregion

namespace Jackie.Utility.Spring
{
    /// <summary> 
    /// Sping Ioc 帮助类
    /// </summary>
    public class SpringHelper
    {
        private volatile static IApplicationContext context;
        private static readonly object LockObj = new object();

        /// <summary>
        /// 获取Context对象
        /// </summary>
        public static IApplicationContext Context
        {
            get
            {

                if (context == null)
                {
                    lock (LockObj)
                    {
                        if (context == null)
                        {
                            context = ContextRegistry.GetContext();
                            var objectConfig = GetObject<IObjectConfig>("ConfigSpringByCode");
                            objectConfig.DefaultConfig();
                            objectConfig.CustomizeConfig();
                        }
                    }
                }
                return context;
            }
        }

        /// <summary>
        /// 创建对象
        /// </summary>
        /// <typeparam name="T">对象类型</typeparam>
        /// <param name="objectId">配置Id</param>
        /// <returns></returns>
        public static T GetObject<T>(string objectId) where T : class
        {
            return Context.GetObjectById<T>(objectId);
        }

        ///<summary>
        /// 创建对象
        ///</summary>
        ///<param name="objectId">配置Id</param>
        ///<param name="arguments">参数</param>
        ///<typeparam name="T">对象类型</typeparam>
        ///<returns></returns>
        public static T GetObject<T>(string objectId, object[] arguments) where T : class
        {
            return Context.GetObject(objectId, arguments) as T;
        }

        /// <summary>
        /// 判断给定配置名称是否已经配置
        /// </summary>
        /// <param name="springObjectId">配置名称</param>
        /// <returns></returns>
        public static bool IsContainsObjectId(string springObjectId)
        {
            return Context.ContainsObject(springObjectId);
        }
    }
}